#!/bin/bash

# Compile engauge-digitizer and bundle it as an AppImage on CentOS 6
#
# This script is supposed to run inside a CentOS 6 environment. On a Ubuntu host machine, I used
# the following commands set up a CentOS 6 environment; you can use your own chroot method of choice instead
# or just boot the CentOS-6.7-$ARCH-LiveCD.iso in a virtual machine and run this script inside there.
# sudo apt-get install unionfs-fuse git
# git clone https://github.com/probonopd/AppImageKit.git
# cp /usr/bin/unionfs-fuse AppImageKit/AppImageAssistant.AppDir/
# sudo linux32 ./AppImageKit/AppImageAssistant.AppDir/testappimage /isodevice/boot/iso/CentOS-6.7-i386-LiveCD.iso bash
# or
# sudo linux64 ./AppImageKit/AppImageAssistant.AppDir/testappimage /isodevice/boot/iso/CentOS-6.7-x86_64-LiveCD.iso bash
# bash -ex ./subsurface.centos6.sh

# Halt on errors
set -e

# Be verbose
set -x

# TODO: move to a library function
git_pull_rebase_helper()
{
	git reset --hard HEAD
        git pull
}

export PATH=/bin:/sbin:$PATH # For CentOS 6

if [ -z "$NO_DOWNLOAD" ] ; then
# Enable EPEL repository; needed for recent Qt and
# install dependencies
sudo yum -y install wget epel-release git make libtool \
        libzip-devel libxml2-devel libxslt-devel libsqlite3x-devel \
        libudev-devel libusbx-devel libcurl-devel libssh2-devel mesa-libGL-devel sqlite-devel \
        tar gzip which make mesa-libEGL coreutils grep wget cmake gcc gcc-c++

# Determine which architecture should be built
if [[ "$(/bin/arch)" = "i686" ||  "$(/bin/arch)" = "x86_64" ]] ; then
	
else
	echo "Architecture could not be determined"
	exit 1
fi

# Now we are inside CentOS 6
grep -r "CentOS release 6" /etc/redhat-release || exit 1

if [ ! -d AppImages ] ; then
  git clone https://github.com/AppImage/AppImages.git
fi
cd AppImages/
git_pull_rebase_helper
cd ..

# EPEL is awesome - fresh Qt5 for old base systems
sudo yum -y install qt5-qtbase-devel  \
                    qt5-qtsvg-devel qt5-linguist qt5-qtconnectivity-devel \
                    qt5-qttools-devel qt5-qttools log4cpp-devel fftw-devel \
                    qt5-qttools-libs-help qt5-qttools-libs-clucene
fi

if [ -z "$NO_DOWNLOAD" ] ; then

wget -q https://github.com/AppImage/AppImages/raw/master/functions.sh -O ./functions.sh
. ./functions.sh

APP=Engauge-Digitizer
rm -rf /$APP/$APP.AppDir
mkdir -p /$APP/$APP.AppDir
cd /$APP

# Get latest engauge-digitizer project from git
if [ ! -d engauge-digitizer ] ; then
  git clone https://github.com/markummitchell/engauge-digitizer.git
fi
cd engauge-digitizer/
git_pull_rebase_helper
cd ..

mkdir -p /$APP/$APP.AppDir/usr/bin

cd /$APP/engauge-digitizer

# Figure out $VERSION
VERSION=$(git rev-parse --short=8 HEAD)
echo $VERSION

export ENGAUGE_RELEASE=1
qmake-qt5 engauge.pro
#Hack to nodebug
sed -i 's/ -g / /g' Makefile
make

cp ./bin/engauge /$APP/$APP.AppDir/usr/bin
cp ./src/img/bannerapp_64.xpm /$APP/$APP.AppDir/engauge.xpm

# Bundle dependency libraries into the AppDir
cd /$APP/$APP.AppDir/
cp ../../AppImageKit/AppRun .
chmod a+x AppRun

#Desktop file
cat > ./engauge.desktop <<\EOF
[Desktop Entry]
Name=Engauge Digitizer
Type=Application
Comment=Extract data from graphs
Exec=engauge
Categories=Education;Science;
Icon=engauge.xpm
EOF

# FIXME: How to find out which subset of plugins is really needed? I used strace when running the binary
mkdir -p ./usr/lib/qt5/plugins/

if [ -e $(dirname /usr/li*/qt5/plugins/bearer) ] ; then
  PLUGINS=$(dirname /usr/li*/qt5/plugins/bearer)
else
  PLUGINS=../../5.5/gc*/plugins/
fi
echo $PLUGINS # /usr/lib64/qt5/plugins if build system Qt is found
cp -r $PLUGINS/bearer ./usr/lib/qt5/plugins/
cp -r $PLUGINS/iconengines ./usr/lib/qt5/plugins/
cp -r $PLUGINS/imageformats ./usr/lib/qt5/plugins/
cp -r $PLUGINS/platforminputcontexts ./usr/lib/qt5/plugins/
cp -r $PLUGINS/platforms ./usr/lib/qt5/plugins/
cp -r $PLUGINS/platformthemes ./usr/lib/qt5/plugins/
#cp -r $PLUGINS/sensors ./usr/lib/qt5/plugins/
cp -r $PLUGINS/xcbglintegrations ./usr/lib/qt5/plugins/

if [ -e $(dirname /usr/li*/libicudata.so.42) ] ; then
  LIB=$(dirname /usr/li*/libicudata.so.42)
else
  LIB=../../5.5/gc*/lib
fi
echo $LIB
cp -a $LIB/libicu* usr/lib

export LD_LIBRARY_PATH=./usr/lib/:../../5.5/gc*/lib/:$LD_LIBRARY_PATH
ldd bin/engauge | grep "=>" | awk '{print $3}'  |  xargs -I '{}' cp -v '{}' ./usr/lib || true
ldd usr/lib/qt5/plugins/platforms/libqxcb.so | grep "=>" | awk '{print $3}'  |  xargs -I '{}' cp -v '{}' ./usr/lib || true

# Copy in the indirect dependencies
FILES=$(find . -type f -executable)

for FILE in $FILES ; do
    ldd "${FILE}" | grep "=>" | awk '{print $3}' | xargs -I '{}' cp -v '{}' usr/lib || true
done

# The following are assumed to be part of the base system
rm -f usr/lib/libcom_err.so.2 || true
rm -f usr/lib/libcrypt.so.1 || true
rm -f usr/lib/libdl.so.2 || true
rm -f usr/lib/libexpat.so.1 || true
rm -f usr/lib/libfontconfig.so.1 || true
rm -f usr/lib/libgcc_s.so.1 || true
rm -f usr/lib/libglib-2.0.so.0 || true
rm -f usr/lib/libgpg-error.so.0 || true
rm -f usr/lib/libgssapi_krb5.so.2 || true
rm -f usr/lib/libgssapi.so.3 || true
rm -f usr/lib/libhcrypto.so.4 || true
rm -f usr/lib/libheimbase.so.1 || true
rm -f usr/lib/libheimntlm.so.0 || true
rm -f usr/lib/libhx509.so.5 || true
rm -f usr/lib/libICE.so.6 || true
rm -f usr/lib/libidn.so.11 || true
rm -f usr/lib/libk5crypto.so.3 || true
rm -f usr/lib/libkeyutils.so.1 || true
rm -f usr/lib/libkrb5.so.26 || true
rm -f usr/lib/libkrb5.so.3 || true
rm -f usr/lib/libkrb5support.so.0 || true
# rm -f usr/lib/liblber-2.4.so.2 || true # needed for debian wheezy
# rm -f usr/lib/libldap_r-2.4.so.2 || true # needed for debian wheezy
rm -f usr/lib/libm.so.6 || true
rm -f usr/lib/libp11-kit.so.0 || true
rm -f usr/lib/libpcre.so.3 || true
rm -f usr/lib/libpthread.so.0 || true
rm -f usr/lib/libresolv.so.2 || true
rm -f usr/lib/libroken.so.18 || true
rm -f usr/lib/librt.so.1 || true
rm -f usr/lib/libsasl2.so.2 || true
rm -f usr/lib/libSM.so.6 || true
rm -f usr/lib/libusb-1.0.so.0 || true
rm -f usr/lib/libuuid.so.1 || true
rm -f usr/lib/libwind.so.0 || true
rm -f usr/lib/libz.so.1 || true

# These seem to be available on most systems but not Ubuntu 11.04
# rm -f usr/lib/libffi.so.6 usr/lib/libGL.so.1 usr/lib/libglapi.so.0 usr/lib/libxcb.so.1 usr/lib/libxcb-glx.so.0 || true

# Delete potentially dangerous libraries
rm -f usr/lib/libstdc* usr/lib/libgobject* usr/lib/libc.so.* || true
# Do NOT delete libX* because otherwise on Ubuntu 11.04:
# loaded library "Xcursor" malloc.c:3096: sYSMALLOc: Assertion (...) Aborted

# We don't bundle the developer stuff
rm -rf usr/include || true
rm -rf usr/lib/cmake || true
rm -rf usr/lib/pkgconfig || true

strip usr/bin/* usr/lib/* || true

# Fix GDK_IS_PIXBUF errors on older distributions

# TODO: Move all cp lib things to using ldconfig because it is more robust 
# across different build system distributions and architectures.
# It is the equivalent for "find" for libraries.
cp $(ldconfig -p | grep libsasl2.so.2 | cut -d ">" -f 2 | xargs) ./usr/lib/
cp $(ldconfig -p | grep libpng12.so.0 | cut -d ">" -f 2 | xargs) ./usr/lib/
# cp $(ldconfig -p | grep libGL.so.1 | cut -d ">" -f 2 | xargs) ./usr/lib/ # otherwise segfaults!?
cp $(ldconfig -p | grep libGLU.so.1 | cut -d ">" -f 2 | xargs) ./usr/lib/ # otherwise segfaults!?
# Fedora 23 seemed to be missing SOMETHING from the Centos 6.7. The only message was:
# This application failed to start because it could not find or load the Qt platform plugin "xcb".
# Setting export QT_DEBUG_PLUGINS=1 revealed the cause.
# QLibraryPrivate::loadPlugin failed on "/usr/lib64/qt5/plugins/platforms/libqxcb.so" : 
# "Cannot load library /usr/lib64/qt5/plugins/platforms/libqxcb.so: (/lib64/libEGL.so.1: undefined symbol: drmGetNodeTypeFromFd)"
# Which means that we have to copy libEGL.so.1 in too
cp $(ldconfig -p | grep libEGL.so.1 | cut -d ">" -f 2 | xargs) ./usr/lib/ # Otherwise F23 cannot load the Qt platform plugin "xcb"
# cp $(ldconfig -p | grep libxcb.so.1 | cut -d ">" -f 2 | xargs) ./usr/lib/ 

# I have no clue why but on i386 systems this seems to be required
if [[ "$ARCH" = "i686" ]] ; then
	cp $(ldconfig -p | grep libgbm.so.1 | cut -d ">" -f 2 | xargs) ./usr/lib/ 
fi

# On openSUSE Qt is picking up the wrong libqxcb.so
# (the one from the system when in fact it should use the bundled one) - is this a Qt bug?
# Hence, we binary patch /usr/lib* to $CWD/lib* which works because at runtime,
# the current working directory is set to usr/ inside the AppImage before running the app
cd usr/ ; find . -type f -exec sed -i -e 's|/usr/lib|././/lib|g' {} \; ; cd ..

cp $(ldconfig -p | grep libfreetype.so.6 | cut -d ">" -f 2 | xargs) ./usr/lib/ # For Fedora 20

get_desktopintegration

delete_blacklisted

# Workaround for:
# symbol lookup error: /usr/lib/x86_64-linux-gnu/gtk-2.0/modules/libunity-gtk-module.so: undefined symbol: g_settings_newn
rm usr/lib/qt5/plugins/platformthemes/libqgtk* || true

# Workaround for:
# GTK theme is broken
rm usr/lib/libgtk* usr/lib/libgdk* usr/lib/libpango* || true

cd ..
find $APP.AppDir/

if [[ "$ARCH" = "x86_64" ]] ; then
	APPIMAGE=$APP"-"$VERSION"-x86_64.AppImage"
fi
if [[ "$ARCH" = "i686" ]] ; then
	APPIMAGE=$APP"-"$VERSION"-i386.AppImage"
fi

# Put this script into the AppImage for debugging
# FIXME: The follwing line does not work
# cp $(readlink --canonicalize $0) ./$APP.AppDir/$APP.recipe

generate_appimage

chmod a+rwx ../out/$APPIMAGE # So that we can edit the AppImage outside of the Docker container
ls -lh ../out/$APPIMAGE
